home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicInternalFrameTitlePane.java < prev    next >
Text File  |  1998-06-30  |  14KB  |  411 lines

  1. /*
  2.  * @(#)OrganicInternalFrameTitlePane.java    1.4 98/04/10
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.event.InternalFrameEvent;
  28. import java.util.EventListener;
  29. import java.beans.PropertyChangeListener;
  30. import java.beans.PropertyChangeEvent;
  31. import java.beans.VetoableChangeListener;
  32. import java.beans.PropertyVetoException;
  33.  
  34. class OrganicActiveInternalFrameButtonUI extends OrganicButtonUI {
  35.     protected Color getSelectColor()       { return OrganicLookAndFeel.getControl3(); }
  36. }
  37.  
  38. class OrganicInactiveInternalFrameButtonUI extends OrganicButtonUI {
  39.     protected Color getSelectColor()       { return OrganicLookAndFeel.getControl2(); }
  40. }
  41.  
  42. class OrganicPaletteButtonUI extends OrganicButtonUI {
  43.     protected Color getSelectColor()       { return OrganicLookAndFeel.getHighlight4(); }
  44. }
  45.  
  46. class OrganicActiveFrameButtonBorder extends OrganicRolloverButtonBorder {    
  47.     protected Color getTopLeftColor()            { return OrganicLookAndFeel.getLightAccent1(); }
  48.     protected Color getBottomRightColor()        { return OrganicLookAndFeel.getControl3(); }
  49.     protected Color getPressedBottomRightColor() { return OrganicLookAndFeel.getControl1(); }
  50. }
  51.  
  52. class OrganicInactiveFrameButtonBorder extends OrganicRolloverButtonBorder {    
  53.     protected Color getTopLeftColor()      { return OrganicLookAndFeel.getWhite(); }
  54.     protected Color getBottomRightColor()  { return OrganicLookAndFeel.getControl3(); }
  55. }
  56.  
  57. class OrganicPaletteButtonBorder extends OrganicRolloverButtonBorder {    
  58.     protected Color getTopLeftColor()            { return OrganicLookAndFeel.getLightAccent1(); }
  59.     protected Color getBottomRightColor()        { return OrganicLookAndFeel.getHighlight4(); }
  60.     protected Color getPressedBottomRightColor() { return OrganicLookAndFeel.getHighlight2(); }
  61. }
  62.  
  63. /**
  64.  * Package private class that manages a organic title bar
  65.  * @version 1.4 04/10/98
  66.  * @author Michael C. Albers
  67.  */
  68. // Could not extend BasicInternalFrameTitlePane because it's private
  69. class OrganicInternalFrameTitlePane extends JComponent 
  70.                                 implements LayoutManager, 
  71.                        ActionListener, 
  72.                        PropertyChangeListener {
  73.  
  74.   protected JMenuBar menuBar;
  75.   protected boolean isPalette = false;                        
  76.   JInternalFrame frame;
  77.   // the three control buttons
  78.   protected OrganicInternalFrameButton iconButton;
  79.   protected OrganicInternalFrameButton maxButton;
  80.   protected OrganicInternalFrameButton closeButton;
  81.   // the rollover borders for these buttons
  82.   protected static Border activeFrameButtonBorder =  new OrganicActiveFrameButtonBorder();
  83.   protected static Border inactiveFrameButtonBorder =  new OrganicInactiveFrameButtonBorder();
  84.   protected static Border paletteButtonBorder =  new OrganicPaletteButtonBorder();
  85.  
  86.   // The different colored UI objects for active and inactive frames
  87.   protected static OrganicActiveInternalFrameButtonUI activeFrameUI = new OrganicActiveInternalFrameButtonUI();
  88.   protected static OrganicInactiveInternalFrameButtonUI inactiveFrameUI = new OrganicInactiveInternalFrameButtonUI();
  89.   protected static OrganicPaletteButtonUI paletteButtonUI = new OrganicPaletteButtonUI();
  90.  
  91.   // the pictures/icons in these three buttons
  92.   Icon maxIcon; 
  93.   Icon altMaxIcon; 
  94.   Icon iconIcon; 
  95.   Icon closeIcon;
  96.  
  97.   final int RESTORE_MENU_ITEM = 0;
  98.   final int MOVE_MENU_ITEM = 1;
  99.   final int SIZE_MENU_ITEM = 2;
  100.   final int MINIMIZE_MENU_ITEM = 3;
  101.   final int MAXIMIZE_MENU_ITEM = 4;
  102.   final int SEPARATOR_MENU_ITEM = 5;
  103.   final int CLOSE_MENU_ITEM = 6;
  104.  
  105.   static Color activeForeground;
  106.   static Color inactiveForeground;
  107.   static Color activeBackground;
  108.   static Color inactiveBackground;                         
  109.   static Color paletteForeground;
  110.   static Color paletteBackground;
  111.     
  112.   public OrganicInternalFrameTitlePane(JInternalFrame f) {
  113.     frame = f;
  114.     //   setPreferredSize(new Dimension(100, 19));
  115.     
  116.     activeForeground = UIManager.getColor("InternalFrameTitlePane.activeForeground");
  117.     inactiveForeground = UIManager.getColor("InternalFrameTitlePane.inactiveForeground");
  118.     activeBackground = UIManager.getColor("InternalFrameTitlePane.activeBackground");
  119.     inactiveBackground = UIManager.getColor("InternalFrameTitlePane.inactiveBackground");                     
  120.     paletteForeground = UIManager.getColor("InternalFrameTitlePane.paletteForeground");
  121.     paletteBackground = UIManager.getColor("InternalFrameTitlePane.paletteBackground");
  122.     maxIcon = (Icon)UIManager.get("InternalFrameTitlePane.maximizeIcon");
  123.     altMaxIcon = (Icon)UIManager.get("InternalFrameTitlePane.altMaximizeIcon");
  124.     iconIcon = (Icon)UIManager.get("InternalFrameTitlePane.iconizeIcon");
  125.     closeIcon = (Icon)UIManager.get("InternalFrameTitlePane.closeIcon");
  126.  
  127.     setFont( UIManager.getFont("InternalFrameTitlePane.font") );
  128.     menuBar = new JMenuBar(){
  129.       public boolean isFocusTraversable() { return false; }
  130.       public void requestFocus() {}
  131.       // PENDING(klobad) Should be able to configure Menu + Button instead
  132.       public void paint(Graphics g) {}
  133.       public boolean isOpaque() { return false; }
  134.     };
  135.     
  136.     menuBar.setBorderPainted(false);
  137.     
  138.     iconButton = new OrganicInternalFrameButton( inactiveFrameUI );
  139.     if(!frame.isIcon())
  140.       iconButton.setIcon(iconIcon);
  141.     else
  142.       iconButton.setIcon(iconIcon);  // iconify
  143.     iconButton.setFocusPainted(false);
  144.     iconButton.addActionListener(this);
  145.     iconButton.setActionCommand("Iconify");
  146.     
  147.  
  148.     maxButton = new OrganicInternalFrameButton( inactiveFrameUI );
  149.     if(!frame.isMaximum())
  150.       maxButton.setIcon(maxIcon);
  151.     else
  152.       maxButton.setIcon(altMaxIcon);
  153.     maxButton.setFocusPainted(false);
  154.     maxButton.addActionListener(this);
  155.     maxButton.setActionCommand("Maximize");
  156.     
  157.  
  158.     closeButton = new OrganicInternalFrameButton( inactiveFrameUI );
  159.     closeButton.setIcon(closeIcon);
  160.     closeButton.setFocusPainted(false);     
  161.     closeButton.addActionListener(this);
  162.     closeButton.setActionCommand("Close");
  163.     
  164.     setLayout(this);
  165.     
  166.     add(menuBar);
  167.     add(iconButton);
  168.     add(maxButton);
  169.     add(closeButton);             
  170.     
  171.     // Make sure these are ok to leave on?
  172.     frame.addPropertyChangeListener(this);
  173.     updateControls();
  174.   }
  175.                          
  176.   public void paint(Graphics g)  {
  177.     boolean isSelected = frame.isSelected();
  178.     Color color = g.getColor();
  179.     
  180.  
  181.     g.setColor(getTitleBarColor());
  182.     g.fillRect(0, 0, getWidth(), getHeight());
  183.     
  184.     if(frame.getTitle() != null) {
  185.       Font f = getFont();
  186.       g.setFont(f);
  187.       FontMetrics fm = g.getFontMetrics();
  188.       int fHeight = fm.getHeight();
  189.  
  190.       int xOffset = 2;
  191.       Icon icon = frame.getFrameIcon();
  192.       if ( icon != null ) {
  193.         xOffset += icon.getIconWidth()+2;
  194.     icon.paintIcon(frame, g, 2, 2);
  195.       }
  196.  
  197.  
  198.       g.setColor(getTitleTextColor());
  199.       int yOffset = 2;
  200.       if (isPalette)
  201.       yOffset++;
  202.       g.drawString(frame.getTitle(),
  203.            menuBar.getX() + menuBar.getWidth() + xOffset,
  204.            fHeight - yOffset);
  205.  
  206.       if (isPalette) {
  207.       g.drawLine(0, fHeight+2, getWidth(), fHeight+2 );
  208.     
  209.       }
  210.       g.setFont(f);
  211.     }
  212.     
  213.     g.setColor(color);
  214.     super.paint(g);
  215.   }
  216.                          
  217.     /**
  218.      * Post a WINDOW_CLOSING-like event to the frame, so that it can
  219.      * be treated like a regular Frame.
  220.      */
  221.     void postClosingEvent(JInternalFrame frame) {
  222.         InternalFrameEvent e = new InternalFrameEvent(
  223.             frame, InternalFrameEvent.INTERNAL_FRAME_CLOSING);
  224.         // Try posting event, unless there's a SecurityManager.
  225.         if (JInternalFrame.class.getClassLoader() == null) {
  226.             try {
  227.                 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
  228.                 return;
  229.             } catch (SecurityException se) {
  230.                 // Use dispatchEvent instead.
  231.             }
  232.         }
  233.         frame.dispatchEvent(e);
  234.     }
  235.  
  236.     public void actionPerformed(ActionEvent e) {
  237.       if("Close".equals(e.getActionCommand()) && frame.isClosable())
  238.           postClosingEvent(frame);
  239.       else if("Iconify".equals(e.getActionCommand()) && frame.isIconifiable()) {
  240.     if(!frame.isIcon())
  241.       try { frame.setIcon(true); } catch (PropertyVetoException e1) { }
  242.     else
  243.       try { frame.setIcon(false); } catch (PropertyVetoException e1) { }
  244.  
  245.       ButtonModel model = iconButton.getModel();
  246.       if ( model != null ) {
  247.           model.setRollover( false );
  248.       }
  249.       } else if("Minimize".equals(e.getActionCommand()) && frame.isMaximizable()) {
  250.     try { frame.setIcon(true); } catch (PropertyVetoException e2) { }
  251.       } else if("Maximize".equals(e.getActionCommand()) && frame.isMaximizable()) {
  252.     if(!frame.isMaximum()) {
  253.       try { frame.setMaximum(true); } catch (PropertyVetoException e5) { }
  254.     } else {
  255.       try { frame.setMaximum(false); } catch (PropertyVetoException e6) { }
  256.     }
  257.       } else if("Restore".equals(e.getActionCommand()) && 
  258.                 frame.isMaximizable() && frame.isMaximum()) {
  259.     try { frame.setMaximum(false); } catch (PropertyVetoException e4) { }
  260.       } else if("Restore".equals(e.getActionCommand()) && 
  261.                 frame.isIconifiable() && frame.isIcon()) {
  262.     try { frame.setIcon(false); } catch (PropertyVetoException e4) { }
  263.       }
  264.     }
  265.                          
  266.     public void propertyChange(PropertyChangeEvent evt) {
  267.       String prop = (String)evt.getPropertyName();
  268.       JInternalFrame f = (JInternalFrame)evt.getSource();
  269.       boolean value = false;
  270.  
  271.       if ( frame.isSelected() ) {
  272.       repaint();
  273.       }
  274.       if(JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) {
  275.       updateControls();
  276.       repaint();
  277.       } else if(JInternalFrame.IS_MAXIMUM_PROPERTY.equals(prop)) {
  278.     value = ((Boolean)evt.getNewValue()).booleanValue();
  279.     if(value)
  280.       maxButton.setIcon(altMaxIcon);
  281.     else
  282.       maxButton.setIcon(maxIcon);  
  283.       } else if(JInternalFrame.IS_ICON_PROPERTY.equals(prop)) {
  284.     value = ((Boolean)evt.getNewValue()).booleanValue();
  285.     if(value)
  286.       iconButton.setIcon(iconIcon);
  287.     else
  288.       iconButton.setIcon(iconIcon);  
  289.       } 
  290.     }
  291.     
  292.     public void addLayoutComponent(String name, Component c) {}
  293.     public void removeLayoutComponent(Component c) {}   
  294.  
  295.    public Dimension preferredLayoutSize(Container c)  {
  296.        return getPreferredSize(c);
  297.     }
  298.  
  299.  
  300.     public Dimension getPreferredSize(Container c)  {
  301.       return new Dimension(c.getSize().width, computeHeight());
  302.     }
  303.  
  304.     public Dimension minimumLayoutSize(Container c) {
  305.       return preferredLayoutSize(c);
  306.     } 
  307.     
  308.     protected int computeHeight() {
  309.       FontMetrics fm =  Toolkit.getDefaultToolkit().getFontMetrics(getFont());
  310.       int fHeight = fm.getHeight();
  311.       fHeight += 3;
  312.       return fHeight;
  313.     }    
  314.                     
  315.     public void layoutContainer(Container c) {
  316.       int w = getWidth();
  317.       int buttonHeight = computeHeight()-2;  // this includes the border
  318.       int buttonWidth = buttonHeight+3;  // this includes the border
  319.  
  320.       int x = (w - buttonWidth) - 1;
  321.       
  322.       if(frame.isClosable()) {
  323.     closeButton.setBounds(x, 1, buttonWidth, buttonHeight);
  324.     x -= buttonWidth;
  325.       } else if(closeButton.getParent() != null) {
  326.     closeButton.getParent().remove(closeButton);
  327.       }
  328.       
  329.       if(frame.isMaximizable()) {
  330.     maxButton.setBounds(x, 1, buttonWidth, buttonHeight);
  331.     x -= buttonWidth;
  332.       } else if(maxButton.getParent() != null) {
  333.     maxButton.getParent().remove(maxButton);
  334.       }
  335.       
  336.       if(frame.isIconifiable()) {
  337.     iconButton.setBounds(x, 1, buttonWidth, buttonHeight);
  338.       } else if(iconButton.getParent() != null) {
  339.     iconButton.getParent().remove(iconButton);
  340.       }
  341.     } 
  342.  
  343.     protected Color getTitleBarColor() {
  344.         Color color;
  345.     if (isPalette)
  346.       color = paletteBackground;
  347.     else {
  348.       if (frame.isSelected())
  349.         color = activeBackground;
  350.       else
  351.         color = inactiveBackground;
  352.     }
  353.     return color;
  354.     }
  355.  
  356.     protected Color getTitleTextColor() {
  357.         Color color;
  358.     if (isPalette)
  359.       color = paletteForeground;
  360.     else {
  361.       if (frame.isSelected())
  362.         color = activeForeground;
  363.       else
  364.         color = inactiveForeground;
  365.     }
  366.     return color;
  367.     }
  368.  
  369.     public void setPalette(boolean b) {
  370.         isPalette = b;
  371.     updateControls();
  372.     }
  373.  
  374.     protected void updateControls() {
  375.       Color background;
  376.       if (isPalette) {
  377.           background = paletteBackground;
  378.           iconButton.setNewUI( paletteButtonUI );
  379.           iconButton.setBorder( paletteButtonBorder );
  380.  
  381.           maxButton.setNewUI( paletteButtonUI );
  382.           maxButton.setBorder( paletteButtonBorder );
  383.           closeButton.setNewUI( paletteButtonUI );
  384.           closeButton.setBorder( paletteButtonBorder );
  385.       } 
  386.       else {
  387.       if ( frame.isSelected() ) {
  388.           background = activeBackground;
  389.           iconButton.setNewUI( activeFrameUI );
  390.           iconButton.setBorder( activeFrameButtonBorder );
  391.           maxButton.setNewUI( activeFrameUI );
  392.           maxButton.setBorder( activeFrameButtonBorder );
  393.           closeButton.setNewUI( activeFrameUI );
  394.           closeButton.setBorder( activeFrameButtonBorder );
  395.       }
  396.       else {
  397.           background = inactiveBackground;
  398.           iconButton.setNewUI( inactiveFrameUI );
  399.           iconButton.setBorder( inactiveFrameButtonBorder );
  400.           maxButton.setNewUI( inactiveFrameUI );
  401.           maxButton.setBorder( inactiveFrameButtonBorder );
  402.           closeButton.setNewUI( inactiveFrameUI );
  403.           closeButton.setBorder( inactiveFrameButtonBorder );
  404.       }
  405.       }
  406.       iconButton.setBackground( background );
  407.       maxButton.setBackground( background );
  408.       closeButton.setBackground( background );
  409.     }                         
  410. };    /// End Title Pane Class
  411.